﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;

namespace ClassLibrary.BLL
{
    public class GioHang
    {
         private Int64 total = 0;
        private int quantity = 0;
        private string primarykey = "";
        private DataTable data;

         public GioHang()
        {
            data = new DataTable();
            data.Columns.Add("STT", typeof(int));
            data.Columns.Add("ID", typeof(string));
            data.Columns.Add("DisplayName", typeof(string));
            data.Columns.Add("Quantity", typeof(int));
            data.Columns.Add("Price", typeof(long));
           // data.Columns.Add("Total",typeof(long));
            data.PrimaryKey = new DataColumn[] { this.data.Columns["ID"] };
        }
         //kiem tra gio hang co' sp nay chua
         public bool Exits(string id)
         {
             foreach (DataRow dong in data.Rows)
             {
                 if (dong["ID"].ToString() == id)
                     return true;
             }
             return false;

         }
         //lay so luong cua 1 san pham
         public int CheckSoluong(string id)
         { 
             DataRow dong = this.data.Rows.Find(id);
             return (int)dong[3];
         }
         //them sp vao gh
         public bool Add(string id, string name, int quant, long price)
         {
             try
             {
                 if (Exits(id))
                 {
                     DataRow dong = data.Rows.Find(id);
                     if (dong != null)
                        
                     {
                         dong["Quantity"] = (int)dong["Quantity"] + quant;
                         return true;
                     }
                     else
                     {
                         return false;
                     }
                 }
                 else
                 {
                     DataRow newrow = this.data.NewRow();
                     newrow["STT"] = this.quantity + 1;
                     newrow["ID"] = id;
                     newrow["DisplayName"] = name;
                     newrow["Quantity"] = quant;
                     newrow["Price"] = price;
                     this.data.Rows.Add(newrow);
                     this.quantity++;
 
                 }
                 return true;
             }
             catch { return false; }
         }

         //xoa sp ra gh
         public bool Remove(string id)
         {
             DataRow dong = data.Rows.Find(id);
             if (dong != null)
             {
                 dong.Delete();
                 this.quantity--;
                 data.AcceptChanges();
                 return true;
             }
             return false;
         }

         //
         private void updateStt()
         {
             int dem = 1;
             foreach (DataRow dong in data.Rows)
             {
                 dong["STT"] = dem;
                 dem++;
             }
         }
         //tinh tong? tien
         private void GetTotal()
         {
             this.total = 0;
             foreach (DataRow dong in data.Rows)
             {
                 this.total += (long)dong["Price"] * (int)dong["Quantity"];
             }
         }
         public Int64 Total
         {
             get { GetTotal(); return this.total; }
         }

         //them so luong

         public bool UpdateQuantity(string id, int quan)
         {
             DataRow dong = this.data.Rows.Find(id);
             if (dong != null)
             {
                 dong[3] = quan;
                 this.data.AcceptChanges();
                 return true;
             }
             return false;
         }
         
         //xoa het gio hang
         public bool Clear()
         {
             this.total = 0;
             this.quantity = 0;
             this.data.Clear();
             return true;
         }

         public DataTable GetProduct(string id)
         {
             DataTable pnew = this.Data.Clone();
             pnew.Clear();
             DataRow dong = this.data.Rows.Find(id);
             if (dong != null)
             {
                 DataRow newrow = pnew.NewRow();
                 newrow["STT"] = "1";
                 newrow["ID"] = dong["ID"].ToString();
                 newrow["DisplayName"] = dong["DisplayName"].ToString();
                 newrow["Quantity"] = dong["Quantity"].ToString();
                 newrow["Price"] = dong["Price"].ToString();
                 if (!pnew.Columns.Contains("Total"))
                 {
                     pnew.Columns.Add("Total", typeof(Int64));
                 }
                 newrow["Total"] = (long)newrow["Price"] * (int)newrow["Quantity"];
                 pnew.Rows.Add(newrow);
             }

             return pnew;
         }

         public int Quantity
         {
             get { return this.quantity; }
         }

         public DataTable Data
         {
             get
             {
                 updateStt();
                 if (!data.Columns.Contains("Total"))
                 {
                     data.Columns.Add("Total", typeof(Int64));
                 }
                 foreach (DataRow dong in this.data.Rows)
                 {
                     dong["Total"] = (long)dong["Price"] * (int)dong["Quantity"];
                 }
                 return this.data;
             }
         }

    }
}
